<template>
  <div class="guide-simple-list">
    <!-- 页面头部 -->
    <div class="page-header">
      <t-button theme="default" variant="text" @click="goBack" class="back-btn">
        <template #icon>
          <t-icon name="chevron-left" />
        </template>
        返回分类
      </t-button>
    </div>

    <!-- 内容区域 -->
    <div class="content-area">
      <!-- 筛选栏 -->
      <div class="filter-bar">
        <t-tabs v-model="activeTab" @change="handleTabChange">
          <t-tab-panel value="0" label="待审核" />
          <t-tab-panel value="2" label="审核完成" />
          <t-tab-panel value="1" label="已打回" />
        </t-tabs>
      </div>

      <!-- 指南列表 -->
      <div class="guide-list" v-if="guideList.length > 0 && !loading">
        <div v-for="guide in guideList" :key="guide.id" class="guide-item" @click="handleGuideClick(guide)">
          <!-- 左侧封面 -->
          <div class="guide-cover">
            <img v-if="guide.imgUrl" :src="guide.imgUrl" :alt="guide.title" class="cover-image" />
            <div v-else class="cover-placeholder">
              <t-icon name="image" />
            </div>
          </div>

          <!-- 中间内容区域 -->
          <div class="guide-content">
            <h3 class="guide-title">{{ guide.title }}</h3>
            <div class="guide-meta">
              <span class="time-info">
                <t-icon name="time" />
                {{ formatRelativeTime(guide.createTime) }}
              </span>
              <span class="category-info">
                <t-icon name="folder" />
                {{ guide.categoryName }}
              </span>
            </div>
          </div>

          <!-- 右侧状态 -->
          <div class="guide-status">
            <t-tag :theme="getStatusTheme(guide.isCheck)" variant="light">
              {{ getStatusText(guide.isCheck) }}
            </t-tag>
            <t-icon name="chevron-right" class="arrow-icon" />
          </div>
        </div>
      </div>

      <!-- 骨架屏 -->
      <div class="guide-list" v-else-if="loading">
        <div v-for="index in 5" :key="index" class="guide-item skeleton-item">
          <!-- 左侧封面骨架屏 -->
          <div class="guide-cover">
            <t-skeleton theme="avatar" :animation="'gradient'" />
          </div>

          <!-- 中间内容区域骨架屏 -->
          <div class="guide-content">
            <t-skeleton theme="text" :animation="'gradient'" :row="2" />
            <div class="guide-meta">
              <t-skeleton theme="text" :animation="'gradient'" :row="1" />
            </div>
          </div>

          <!-- 右侧状态骨架屏 -->
          <div class="guide-status">
            <t-skeleton theme="text" :animation="'gradient'" :row="1" />
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-else class="empty-state">
        <t-empty
          v-if="activeTab === '1'"
          description="暂无指南数据，用户可能已经重新提交审核了，请前往审核页面查看，或者已经删除"
        >
          <template #description>
            <p>当前筛选条件下暂无指南数据</p>
          </template>
        </t-empty>
        <t-empty v-else description="暂无指南数据">
          <template #description>
            <p>当前筛选条件下暂无指南数据</p>
          </template>
        </t-empty>
      </div>

      <!-- 分页 -->
      <div class="pagination-wrapper" v-if="total > 0">
        <t-pagination
          v-model="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-size-options="[5, 10, 20, 50]"
          @change="handlePageChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getGuideSimpleList } from '@/api/guide';
import type { GuideResponse } from '@/api/model/guideModel';
import { formatRelativeTime } from '@/utils/date';
import { MessagePlugin } from 'tdesign-vue-next';
import { onActivated, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
// 路由实例
const router = useRouter();
const route = useRoute();

// 响应式数据
const categoryId = ref<string>('');
const categoryName = ref<string>('');
const currentPath = ref<string>('');

// 列表数据
const guideList = ref<GuideResponse[]>([]);
const total = ref<number>(0);
const currentPage = ref<number>(1);
const pageSize = ref<number>(10);
const loading = ref<boolean>(false);

// 筛选状态
const activeTab = ref<string>('0'); // 默认显示待审核

// 返回上一页
const goBack = () => {
  router.push('/guide/audit');
};

// 处理标签页切换
const handleTabChange = (value: string) => {
  activeTab.value = value;
  if (value === '1') {
    MessagePlugin.info('用户重新提交审核不会在此列表中显示');
  }
  currentPage.value = 1; // 重置到第一页
  loadGuideList();
};

// 处理分页变化
const handlePageChange = (pageInfo: { current: number; pageSize: number }) => {
  console.log('pageInfo', pageInfo);
  currentPage.value = pageInfo.current;
  pageSize.value = pageInfo.pageSize;
  loadGuideList();
};

// 处理指南点击
const handleGuideClick = (guide: GuideResponse) => {
  // 跳转到指南详情页面
  router.push({
    name: 'GuideDetail',
    query: {
      id: guide.id.toString(),
      from: 'GuideSimpleList',
      activeTab: activeTab.value,
    },
  });
};

// 加载指南列表
const loadGuideList = async () => {
  try {
    loading.value = true;

    const params = {
      categoryId: parseInt(categoryId.value),
      isCheck: parseInt(activeTab.value), // 0: 待审核, 1: 已打回, 2: 审核完成
      pageNum: currentPage.value,
      pageSize: pageSize.value,
    };

    const response = await getGuideSimpleList(params);
    guideList.value = response.data.data.records;
    total.value = response.data.data.total;
  } catch (error) {
    console.error('加载指南列表失败:', error);
    MessagePlugin.error('获取指南列表失败，请稍后重试');
  } finally {
    loading.value = false;
  }
};

// 获取状态文本
const getStatusText = (isCheck: number) => {
  switch (isCheck) {
    case 0:
      return '待审核';
    case 1:
      return '已打回';
    case 2:
      return '审核完成';
    default:
      return '待审核';
  }
};

// 获取状态主题
const getStatusTheme = (isCheck: number) => {
  switch (isCheck) {
    case 0:
      return 'warning';
    case 1:
      return 'danger';
    case 2:
      return 'success';
    default:
      return 'warning';
  }
};

// 页面初始化
onActivated(async () => {
  // 获取路由参数
  categoryId.value = (route.query.categoryId as string) || '';
  categoryName.value = (route.query.categoryName as string) || '未知分类';
  currentPath.value = route.path;
  activeTab.value = (route.query.activeTab as string) || '0';

  // 设置加载状态
  loading.value = true;

  // 加载指南列表
  await loadGuideList();
});
</script>

<style scoped>
.guide-simple-list {
  padding: 24px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

/* 页面头部样式 */
.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  gap: 16px;
}

.back-btn {
  flex-shrink: 0;
}

.page-subtitle {
  font-size: 14px;
  color: #86909c;
  margin: 0;
  line-height: 1.5;
}

/* 内容区域样式 */
.content-area {
  max-width: 800px;
  margin: 0 auto;
}

/* 筛选栏样式 */
.filter-bar {
  background: white;
  border-radius: 8px;
  margin-bottom: 16px;
  padding: 0 16px;
}

/* 指南列表样式 */
.guide-list {
  background: white;
  border-radius: 8px;
  overflow: hidden;
}

.guide-item {
  display: flex;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background-color 0.2s;
}

.guide-item:hover {
  background-color: #f8f9fa;
}

.guide-item:last-child {
  border-bottom: none;
}

/* 左侧封面样式 */
.guide-cover {
  width: 80px;
  height: 80px;
  margin-right: 16px;
  flex-shrink: 0;
}

.cover-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

.cover-placeholder {
  width: 100%;
  height: 100%;
  background: #e7f3ff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #0052d9;
  font-size: 24px;
}

/* 中间内容区域样式 */
.guide-content {
  flex: 1;
  min-width: 0;
}

.guide-title {
  font-size: 16px;
  font-weight: 500;
  color: #1d2129;
  margin: 0 0 8px 0;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.guide-meta {
  display: flex;
  align-items: center;
  gap: 16px;
  font-size: 12px;
  color: #86909c;
}

.time-info,
.category-info {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 右侧状态样式 */
.guide-status {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.arrow-icon {
  color: #c9cdd4;
  font-size: 16px;
}

/* 空状态样式 */
.empty-state {
  background: white;
  border-radius: 8px;
  padding: 40px 20px;
  text-align: center;
}

/* 分页样式 */
.pagination-wrapper {
  background: white;
  border-radius: 8px;
  padding: 16px;
  margin-top: 16px;
  display: flex;
  justify-content: center;
}

/* 骨架屏样式 */
.skeleton-item {
  cursor: default;
}

.skeleton-item:hover {
  background-color: transparent;
}

.skeleton-item .guide-cover {
  display: flex;
  align-items: center;
  justify-content: center;
}

.skeleton-item .guide-content {
  padding: 0 8px;
}

.skeleton-item .guide-meta {
  margin-top: 8px;
}

.skeleton-item .guide-status {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 80px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .guide-simple-list {
    padding: 16px;
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .page-title {
    font-size: 20px;
  }

  .guide-item {
    padding: 12px;
  }

  .guide-cover {
    width: 60px;
    height: 60px;
    margin-right: 12px;
  }

  .guide-title {
    font-size: 14px;
  }

  .guide-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}
</style>
